/*
 * Task.cpp
 *
 *  Created on: Oct 18, 2012
 *      Author: david
 */

#include "Task.h"

Task::Task() {
	depth = EMPTY;
	numOfNodes = 0;
	maxJob = NULL;
	actualState = NULL;
}

Task::~Task() {
	delete maxJob;
	delete actualState;
}

int Task::nexByInreaseDepth() {
	if (logTaskStartNodeInfo && depth == 0) cout << "starting with node: " << actualState[depth] << endl;
	if (depth < numOfNodes) {
		if (logTask) logPrintMessage("I'll increase depth");
		depth++;
		actualState[depth] = actualState[depth - 1];
		if (logTask) logPrintTask();
		return nextByWidth();
	}
	if (logTask) logPrintMessage("cannot increase depth");
	return EMPTY;
}

void Task::decreaseDepth() {
	if (logTask) logPrintMessage("I'll decrease depth");
	if (depth >= 0) {
		actualState[depth] = EMPTY;
		depth--;
	}
	if (logTask) logPrintTask();
}

int Task::getDepth() {
	return depth;
}

int* Task::getActualState() {
	return actualState;
}

int Task::nextByWidth() {
	if (actualState[depth] < maxJob[depth]) {
		if (logTask) logPrintMessage("I'll go to width");
		actualState[depth] = actualState[depth] + 1;
		if (logTask) logPrintTask();
		return actualState[depth];
	}
	if (logTask) logPrintMessage("cannot go to width");
	return EMPTY;
}

void Task::init() {
	int i = 0;
	while (actualState[i] == EMPTY) {
		i++;
	}
	depth = i;
	if (logTask) {
		logPrintMessage("task initialized");
		logPrintTask();
	}
}

bool Task::haveWork() {
	return depth == -1 ? false : true;
}

void Task::createFullTask(int numberOfNodes) {
	numOfNodes = numberOfNodes;
	actualState = new int[numOfNodes];
	maxJob = new int[numOfNodes];

	for (int i = 0; i < numOfNodes; ++i) {
		maxJob[i] = numOfNodes - 1;
		actualState[i] = EMPTY;
	}
	if (numOfNodes <= 0) {
		throw new exception();
	}
	actualState[0] = 0;

	init();
}

Task** Task::split(int numbterOfNewTasks) {

	//Task** tasks;
	//TODO
	Task* task = new Task();
	delete task;
	return NULL;
}

void Task::logPrintTask() {
	cout << "Task state. depth is: " << depth << endl;
	for (int i = 0; i < numOfNodes; ++i) {
		printf(" %5d , %5d\n", actualState[i], maxJob[i]);
	}
}
